package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 套餐服务的实现类
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Resource(name = "setmealMapper")
    SetmealMapper setmealMapper;
    @Resource(name = "setmealDishMapper")
    SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     * @param setmealDTO 套餐信息DTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        // 数据拷贝
        BeanUtils.copyProperties(setmealDTO, setmeal);

        // 套餐表增加数据
        setmealMapper.addSetmeal(setmeal);

        // 利用数据库自动生成的主键给setmealDishes中的每一个setmealId赋值
        setmealDishList.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));

        // 套餐菜品关系表批量增加数据
        setmealDishMapper.addsetmealDish(setmealDishList);
    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO 查询条件DTO
     * @return 分页结果
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        List<Setmeal> setmealList = setmealMapper.pageQuery(setmealPageQueryDTO);
        Page<Setmeal> setmealPage = (Page<Setmeal>)setmealList;
        return new PageResult(setmealPage.getTotal(),setmealPage.getResult());
    }

    /**
     * 设置套餐的启用或停用状态
     * @param status 状态值：1为启用，0为停用
     * @param id 套餐ID
     */
    @Override
    public void setmealStopAndStart(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
        setmealMapper.updateTimeSetmeal(setmeal);
    }

    /**
     * 修改套餐信息
     * @param setmealDTO 套餐信息DTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTimeSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 修改套餐表
        setmealMapper.updateTimeSetmeal(setmeal);

        // 修改套餐菜品关系表
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes();
        setmealDishList.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
        // 通过套餐id批量删除套餐菜品关系表中的数据
        setmealDishMapper.delSetmealDishBySetmeaId(setmealDishList);
        // 重新插入套餐id的数据到套餐菜品关系表
        setmealDishMapper.addsetmealDish(setmealDishList);
    }

    /**
     * 根据套餐ID查询套餐详情
     * @param id 套餐ID
     * @return 套餐详情VO
     */
    @Override
    public SetmealVO selSetmealById(Long id) {
        // 通过套餐id查询套餐内容
        SetmealVO setmealVO =  setmealDishMapper.selSetmealById(id);
        // 通过套餐id查询套餐内的菜品
        List<SetmealDish> setmealDish = setmealDishMapper.selSetmealDishById(id);
        setmealVO.setSetmealDishes(setmealDish);
        return setmealVO;
    }

    /**
     * 批量删除套餐
     * @param ids 套餐ID列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delSetmeals(List<Long> ids) {
        // 如果在售提示在售不能删除
        List<Setmeal> statusStartList = setmealMapper.selSetmealById(ids);
        statusStartList.forEach(setmeal -> {
            if (setmeal.getStatus() == 1) throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        });
        // 删除套餐表的内容
        setmealMapper.delSetmeals(ids);
        // 删除套餐口味表的内容
        setmealDishMapper.delSetmeals(ids);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
